#include "mvc/view/principal_window.h"
#include "ui_principal_window.h"

#include <QToolBar>
#include <QIcon>
#include <QMessageBox>
#include <QFileDialog>
#include <QtGui>
#include "common/settings.h"
#include <iostream>

using std::cout;
using std::endl;

Principal_window::Principal_window(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Principal_window)
{
    ui->setupUi(this);
    create_actions();
    create_menus();
    create_toolbars();
    retranslateUi();
    disable_simulator_actions();
}

Principal_window::~Principal_window()
{
    delete ui;
}

/**
  * Add the 'widget' parameter as main window's central widget.
  */
void Principal_window::add_central_widget(QWidget *widget)
{
    widget->resize(this->width()-10,this->height()-100);
    scrollArea.setWidget(widget);
    this->setCentralWidget(&scrollArea);
}
/**
  * Return the 'factor' value.
  */
int Principal_window::get_factor(){
    bool ok;
    int result = factor_spinbox->text().toInt(&ok, 10);
    if (ok)
        return result;
    else{
        std::cout << "ERROR IN GET_FACTOR  (principal_window.cpp)" << std::endl;
        exit(0);
    }
}
/**
  * Return the 'paused' value.
  */
bool Principal_window::get_paused(){
    return paused;
}

/**
  * Disable all simulator actions.
  */
void Principal_window::disable_simulator_actions(){
    factor_label->setDisabled(true);
    factor_spinbox->setDisabled(true);
    real_time->setDisabled(true);
    sim_step->setDisabled(true);
    ui->play_pause_action->setDisabled(true);
    ui->rewind_action->setDisabled(true);
}

/**
  * Enable all simulator actions.
  */
void Principal_window::enable_simulator_actions(){
    factor_label->setEnabled(true);
    factor_spinbox->setEnabled(true);
    real_time->setEnabled(true);
    sim_step->setEnabled(true);
    ui->play_pause_action->setEnabled(true);
    ui->play_pause_action->setIcon(QIcon(tr(":images/play.png")));
    ui->rewind_action->setEnabled(true);
    paused = false;
}

/**
  * Set time for real_time label
  */
void Principal_window::set_real_time(double time){
    QString text = tr("Real time: ");
    text.append(QString::number(time));
    text.append(tr(" seconds "));
    real_time->setText(text);
}

/**
  * Set step for sim_step label
  */
void Principal_window::set_sim_step(int step){
    QString text = tr("Simulation step: ");
    text.append(QString::number(step));
    sim_step->setText(text);
}

/**
  * Enable view actions in the window
  */
void Principal_window::enable_view_actions()
{
    ui->uavtex_action->setEnabled(true);
    ui->simtex_action->setEnabled(true);
    ui->perspective_action->setEnabled(true);
    ui->ortho_action->setEnabled(true);
}

/**
 * @brief Principal_window::enable_graphic_allc
 */
void Principal_window::enable_graphic_allc()
{
    ui->actionAll_configurations->setEnabled(true);
}

// PRIVATE SLOTS

/**
  * Emit the signal of new project to the view component.
  */
void Principal_window::new_project()
{
    emit s_new_project();
}

/**
  * Emit the signal open project to the view component.
  */
void Principal_window::open_project()
{
    //std::cout << "TODO: Open Project" << std::endl;
    //if(ok_to_continue())
    //{
    QString _projects = projects_directory();
    QString filters = tr("(*.uav)\n (*.conventional)\n (*.bwb)\n All files (*)");
    QString file_name = QFileDialog::getOpenFileName(this, tr("Open Project"), _projects, filters);

    if(!file_name.isEmpty())
        emit s_open_project(file_name);
    //}
}

/**
  * Emit the signal settings to the view component.
  */
void Principal_window::settings(){
    emit s_settings();
}

/**
  * Actions when About option is selected
  */
void Principal_window::about()
{
    //    std::cout << "TODO: About" << std::endl;
    QMessageBox::about(this, tr("About GePar"),
                       tr("<h2>GePar 1.0</h2>"
                          "<p>Gepar is an application to generates geometries "
                          "of Unmanned Aerial Vehicles (UAVs) with Joined Wings "
                          "and Aircrafts with conventional wings. "
                          ));
}

/**
  * Actions when Play button is pressed
  */
void Principal_window::play_pressed(){
    if (paused)
        ui->play_pause_action->setIcon(QIcon(tr(":images/play.png")));
    else
        ui->play_pause_action->setIcon(QIcon(tr(":images/pause.png")));
    paused = !paused;
    emit s_play_pause();
}

/**
  * Actions when Perspective option is selected
  */
void Principal_window::perspective()
{
    bool is_checked = ui->ortho_action->isChecked();
    ui->ortho_action->setChecked(!is_checked);

    if(is_checked)
        emit s_perspective();
    else
        emit s_ortho();
}

/**
  * Actions when Ortho option is selected
  */
void Principal_window::ortho()
{
    bool is_checked = ui->perspective_action->isChecked();
    ui->perspective_action->setChecked(!is_checked);

    if(is_checked)
        emit s_ortho();
    else
        emit s_perspective();
}

/**
  * Actions when English option is selected
  */
void Principal_window::english_selected(){
    bool is_checked = ui->language_English->isChecked();
    ui->language_Spanish->setChecked(!is_checked);
    if (is_checked)
        emit s_english();
    else
        emit s_spanish();

}

/**
  * Actions when Spanish option is selected
  */
void Principal_window::spanish_selected()
{
    bool is_checked = ui->language_Spanish->isChecked();
    ui->language_English->setChecked(!is_checked);
    if (is_checked)
        emit s_spanish();
    else
        emit s_english();
}


// PRIVATE METHODS

/**
  * Create the Factor's Label and Factor's LineEdit
  */
void Principal_window::initialize_labels(){
    factor_label = new QLabel();

    factor_spinbox = new QSpinBox;
    factor_spinbox->setMaximum(999);
    factor_spinbox->setMinimum(1);
    factor_spinbox->setFixedWidth(60);
    factor_spinbox->setValue(100);
    factor_spinbox->setSingleStep(10);

    real_time = new QLabel();
    real_time->setFixedWidth(250);
    sim_step = new QLabel();

}

/**
  * Create the file menu actions
  */
void Principal_window::create_actions()
{
    ui->new_action->setIcon(QIcon(tr(":images/add.png")));
    ui->new_action->setShortcut(tr("Ctrl+N"));

    ui->open_action->setIcon(QIcon(tr(":images/open.png")));
    ui->open_action->setShortcut(tr("Ctrl+O"));

    connect(ui->new_action, SIGNAL(triggered()), this, SLOT(new_project()));
    connect(ui->open_action, SIGNAL(triggered()), this, SLOT(open_project()));

    for(int i = 0; i < MAX_RECENT_PROJECTS; i++)
    {
        recent_project_actions[i] = new QAction(this);
        recent_project_actions[i]->setVisible(false);
        connect(recent_project_actions[i], SIGNAL(triggered()), this, SLOT(open_project()));
    }

    connect(ui->about_action, SIGNAL(triggered()), this, SLOT(about()));

    exit_action = new QAction(this);
    exit_action->setIcon(QIcon(tr(":images/exit.png")));
    exit_action->setShortcut(tr("Ctrl+E"));

    connect(exit_action, SIGNAL(triggered()), this, SLOT(close()));

    ui->settings_action->setIcon(QIcon(tr(":images/settings.png")));
    connect(ui->settings_action, SIGNAL(triggered()), this, SLOT(settings()));
}

/**
  * Create the menu
  */
void Principal_window::create_menus()
{

    ui->menuFile->addSeparator();

    for(int i = 0; i < MAX_RECENT_PROJECTS; i++)
        ui->menuFile->addAction(recent_project_actions[i]);

    ui->menuFile->addSeparator();
    ui->menuFile->addAction(exit_action);

    // CREATE EDIT
    ui->rrotate_action->setIcon(QIcon(tr(":images/r_rotate.png")));
    //ui->rrotate_action->setStatusTip(tr("Rotate to right"));
    connect(ui->rrotate_action, SIGNAL(triggered()), this, SIGNAL(s_rrotate()));

    ui->lrotate_action->setIcon(QIcon(tr(":images/l_rotate.png")));
    //ui->lrotate_action->setStatusTip(tr("Rotate to left"));
    connect(ui->lrotate_action, SIGNAL(triggered()), this, SIGNAL(s_lrotate()));

    //ui->urotate_action->setIcon(QIcon(":images/u_rotate.png"));
    //ui->urotate_action->setStatusTip(tr("Rotar 90º hacia arriba"));
    connect(ui->urotate_action, SIGNAL(triggered()), this, SIGNAL(s_urotate()));

    //ui->drotate_action->setIcon(QIcon(":images/d_rotate.png"));
    //ui->drotate_action->setStatusTip(tr("Rotar 90º hacia abajo"));
    connect(ui->drotate_action, SIGNAL(triggered()), this, SIGNAL(s_drotate()));

    ui->zout_action->setIcon(QIcon(tr(":images/zoom_out.png")));
    ui->zout_action->setShortcut(tr("-"));
    //ui->zout_action->setStatusTip(tr("Zoom out"));
    connect(ui->zout_action, SIGNAL(triggered()), this, SIGNAL(s_zout()));

    ui->zin_action->setIcon(QIcon(tr(":images/zoom_in.png")));
    ui->zin_action->setShortcut(tr("+"));
    //ui->zin_action->setStatusTip(tr("Zoom in"));
    connect(ui->zin_action, SIGNAL(triggered()), this, SIGNAL(s_zin()));

    ui->umove_action->setIcon(QIcon(tr(":images/up.png")));
    ui->umove_action->setShortcut(tr("u"));
    //ui->umove_action->setStatusTip(tr("Move to up"));
    connect(ui->umove_action, SIGNAL(triggered()), this, SIGNAL(s_umove()));

    ui->dmove_action->setIcon(QIcon(tr(":images/down.png")));
    ui->dmove_action->setShortcut(tr("d"));
    //ui->dmove_action->setStatusTip(tr("Move to down"));
    connect(ui->dmove_action, SIGNAL(triggered()), this, SIGNAL(s_dmove()));

    ui->rmove_action->setIcon(QIcon(tr(":images/right.png")));
    ui->rmove_action->setShortcut(tr("r"));
    //ui->rmove_action->setStatusTip(tr("Move to right"));
    connect(ui->rmove_action, SIGNAL(triggered()), this, SIGNAL(s_rmove()));

    ui->lmove_action->setIcon(QIcon(tr(":images/left.png")));
    ui->lmove_action->setShortcut(tr("l"));
    //ui->lmove_action->setStatusTip(tr("Move to left"));
    connect(ui->lmove_action, SIGNAL(triggered()), this, SIGNAL(s_lmove()));

    ui->reset_action->setIcon(QIcon(tr(":images/reset.png")));
    //ui->reset_action->setStatusTip(tr("Reset view"));
    connect(ui->reset_action, SIGNAL(triggered()), this, SIGNAL(s_reset_camera()));

    ui->play_pause_action->setIcon(QIcon(tr(":images/play.png")));
    //ui->play_pause_action->setStatusTip(tr("Play/Pause"));
    connect(ui->play_pause_action, SIGNAL(triggered()), this, SLOT(play_pressed()));

    ui->rewind_action->setIcon(QIcon(tr(":images/rewind.png")));
    //ui->rewind_action->setStatusTip(tr("Rewind"));
    connect(ui->rewind_action, SIGNAL(triggered()), this, SIGNAL(s_rewind()));

    // CREATE CONFIG
    //ui->uavtex_action->setStatusTip(tr("Uav Texture"));
    connect(ui->uavtex_action, SIGNAL(triggered()), this, SIGNAL(s_uavtex()));

    //ui->simtex_action->setStatusTip(tr("Sim Texture"));
    connect(ui->simtex_action, SIGNAL(triggered()), this, SIGNAL(s_simtex()));

    //ui->perspective_action->setStatusTip(tr("Perspective view"));
    connect(ui->perspective_action, SIGNAL(triggered()), this, SLOT(perspective()));

    //ui->ortho_action->setStatusTip(tr("Ortho view"));
    connect(ui->ortho_action, SIGNAL(triggered()), this, SLOT(ortho()));

    connect(ui->language_English, SIGNAL(triggered()), this, SLOT(english_selected()));

    connect(ui->language_Spanish, SIGNAL(triggered()), this, SLOT(spanish_selected()));

    // CREATE TOOLS
    ui->actionGeometry->setIcon(QIcon(":images/geometryGraphic.jpg"));

    //ui->actionGeometry->setStatusTip(tr(""));
    connect(ui->actionGeometry, SIGNAL(triggered()), this, SIGNAL(s_graphicGeometry()));

    ui->actionAll_configurations->setIcon(QIcon(":images/allconfigurationsGraphic.png"));
    //ui->actionAll_configurations->setStatusTip(tr(""));
    connect(ui->actionAll_configurations, SIGNAL(triggered()), this, SIGNAL(s_graphicAllConfigurations()));

    // CREATE HELP
}

/**
  * Create the toolbars
  */
void Principal_window::create_toolbars()
{

    project_toolbar = addToolBar(tr("&Project"));
    initialize_labels();

    project_toolbar->addAction(ui->new_action);
    project_toolbar->addAction(ui->open_action);
    project_toolbar->addSeparator();
    project_toolbar->addAction(ui->settings_action);

    project_toolbar->addSeparator();
    project_toolbar->addAction(ui->rrotate_action);
    project_toolbar->addAction(ui->lrotate_action);
    //    project_toolbar->addAction(ui->urotate_action);
    //    project_toolbar->addAction(ui->drotate_action);
    project_toolbar->addAction(ui->zout_action);
    project_toolbar->addAction(ui->zin_action);
    project_toolbar->addAction(ui->umove_action);
    project_toolbar->addAction(ui->dmove_action);
    project_toolbar->addAction(ui->rmove_action);
    project_toolbar->addAction(ui->lmove_action);
    project_toolbar->addAction(ui->reset_action);

    project_toolbar->addSeparator();
    project_toolbar->addAction(ui->actionGeometry);
    project_toolbar->addAction(ui->actionAll_configurations);

    project_toolbar->addSeparator();
    project_toolbar->addWidget(factor_label);
    project_toolbar->addWidget(factor_spinbox);
    project_toolbar->addAction(ui->play_pause_action);
    project_toolbar->addAction(ui->rewind_action);
    project_toolbar->addWidget(real_time);
    project_toolbar->addWidget(sim_step);
}

/**
  *
  */
void Principal_window::closeEvent(QCloseEvent *ev)
{
    //cout << "TODO: Principal_window.closeEvent" << endl;
    emit s_close(ev);
    //QMainWindow::closeEvent(close);
}

/*
 * Set translatable text in the window
 */
void Principal_window::retranslateUi(){
    factor_label->setText(tr("Display time factor:"));
    real_time->setText(tr("Real time: "));
    sim_step->setText(tr("Simulation step: 0"));
    exit_action->setText(tr("&Exit"));
    ui->new_action->setStatusTip(tr("New project"));
    ui->open_action->setStatusTip(tr("Open project"));
    ui->about_action->setStatusTip(tr("About..."));
    ui->menuFile->setTitle(tr("&Project"));
    ui->drotate_action->setShortcut(tr("down"));
    ui->rrotate_action->setShortcut(tr("right"));
    ui->lrotate_action->setShortcut(tr("left"));
    ui->urotate_action->setShortcut(tr("up"));
    ui->retranslateUi(this);
}
